<?php
include('../../includes/classes/core.php');



$start = $_REQUEST['start'];
$end   = $_REQUEST['end'];


$action = $_REQUEST['act'];

$result = mysql_query("SELECT 		CONCAT(production_category.`name`, '<br>', ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2), ' ლ  ', '(',SUM(sale_transaction_detail.product_quantity),'ც)') AS `category_name`,
					SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost) AS `quantity`
					FROM sale_transaction
					LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
					LEFT JOIN production ON production.id = sale_transaction_detail.production_id
					LEFT JOIN production_category ON production.pos_menu_category = production_category.id
					WHERE production_category.`name` IS NOT NULL AND `sale_transaction`.`status`=0 AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end'
					GROUP BY production.`pos_menu_category`");

function GetProducts($name, $start, $end){
	$res = mysql_query("SELECT 	CONCAT(production.`name`, '<br>', ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2), ' ლ ', '(',SUM(sale_transaction_detail.product_quantity),'ც)') AS `prod_name`,
			SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost) AS `quantity`
			FROM sale_transaction
			LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
			LEFT JOIN production ON production.id = sale_transaction_detail.production_id
			LEFT JOIN production_category ON production.pos_menu_category = production_category.id
			WHERE production_category.`name` = '$name' AND `sale_transaction`.`status`=0 AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end'
			GROUP BY production.`name`");
			return $res;
}


switch ($action) {
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = $r[1];
			array_push($rows,$row);
		}

		print json_encode($rows, JSON_NUMERIC_CHECK);
		break;
	case 'get_products' :
		$name = $_REQUEST['name'];

		$res = GetProducts($name, $start, $end);
		$rows = array();
		while($r = mysql_fetch_array($res)) {
			$row[0] = $r[0];
			$row[1] = $r[1];
			array_push($rows,$row);
		}

		print json_encode($rows, JSON_NUMERIC_CHECK);
		break;

	default :
		echo "Action Is Null!";
		break;

}



?>